cmake_minimum_required(VERSION 3.14.1)
project(demo) #${PROJECT_NAME}  demo


#让cmake知道到include目录下找头文件
include_directories(${CMAKE_CURRENT_LIST_DIR}/include)

#让cmake知道到lib下面找库文件
link_directories(${CMAKE_CURRENT_LIST_DIR}/lib)

#将src下面的源文件放到变量${SRC}中
aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/src SRC)

#生成静态库
add_library(${PROJECT_NAME}_static STATIC ${CMAKE_CURRENT_LIST_DIR}/src/test.cpp)
set_target_properties(${PROJECT_NAME}_static PROPERTIES ARCHIVE_OUTPUT_NAME "test")
install(TARGETS ${PROJECT_NAME}_static ARCHIVE DESTINATION ${CMAKE_CURRENT_LIST_DIR}/lib)

add_library(${PROJECT_NAME}_static2 STATIC ${CMAKE_CURRENT_LIST_DIR}/src/static_test.cpp)
set_target_properties(${PROJECT_NAME}_static2 PROPERTIES ARCHIVE_OUTPUT_NAME "test2")
install(TARGETS ${PROJECT_NAME}_static2 ARCHIVE DESTINATION ${CMAKE_CURRENT_LIST_DIR}/lib)

#生成动态库
#[[
add_library(${PROJECT_NAME}_shared SHARED ${CMAKE_CURRENT_LIST_DIR}/src/test.cpp)
set_target_properties(${PROJECT_NAME}_shared PROPERTIES OUTPUT_NAME "test")
install(TARGETS ${PROJECT_NAME}_shared LIBRARY DESTINATION ${CMAKE_CURRENT_LIST_DIR}/lib)

add_library(${PROJECT_NAME}_shared2 SHARED ${CMAKE_CURRENT_LIST_DIR}/src/shared_test.cpp)
set_target_properties(${PROJECT_NAME}_shared2 PROPERTIES OUTPUT_NAME "test2")
install(TARGETS ${PROJECT_NAME}_shared2 LIBRARY DESTINATION ${CMAKE_CURRENT_LIST_DIR}/lib)
]]

#生成可执行文件
add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR}/src/main.cpp)
install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION ${CMAKE_CURRENT_LIST_DIR}/lib)

#一句话：静态库文件查找符号的时候从它后面加载的库文件中进行搜索
#静态库默认会进行符号优化
target_link_libraries(${PROJECT_NAME} ${PROJECT_NAME}_static)
target_link_libraries(${PROJECT_NAME} ${PROJECT_NAME}_static2)

#动态库默认会带入全部符号，所以不存在前后依赖的问题
#target_link_libraries(${PROJECT_NAME} ${PROJECT_NAME}_shared)
#target_link_libraries(${PROJECT_NAME} ${PROJECT_NAME}_shared2)


#设置编译器编译选项
set(CMAKE_CXX_COMPILER "g++")
set(CMAKE_CXX_FLAGS "-std=c++11")
set(CMAKE_CXX_FLAGS "-Wall")
set(CMAKE_CXX_FLAGS "-Wall")
set(CMAKE_CXX_FLAGS_DEBUG "-O0")
set(CMAKE_CXX_FLAGS_RELEASE "-O2-DNDEBUG")
